<template>
  <el-dialog title="系统发药" :close-on-click-modal="false" :visible.sync="visible" center append-to-body>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <div class="avue-crud">
      处方信息
    </div>
    <div class="avue-crud">
      <el-table :data="dataList" border>
        <el-table-column prop="medicineName" header-align="center" align="center" label="药品名称"></el-table-column>
        <el-table-column prop="usages" header-align="center" align="center" label="用法"></el-table-column>
        <el-table-column prop="usageLocation" header-align="center" align="center" label="用药部位"></el-table-column>
        <el-table-column prop="metering" header-align="center" align="center" label="单次剂量"></el-table-column>
        <el-table-column prop="frequency" header-align="center" align="center" label="频次"></el-table-column>
        <el-table-column prop="money" header-align="center" align="center" label="金额"></el-table-column>
      </el-table>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="takeMedicine()">确定发药</el-button>
    </span>

    <!-- 弹窗, 系统发药 -->
    <table-form v-if="takeMedicineVisible" ref="takeMedicine"></table-form>
  </el-dialog>

</template>

<script>
  import {
    getPrescriptionObj,
    addEimsOutpatientPrescriptionObj,
    putEimsOutpatientPrescriptionObj
  } from '@/api/eims/prescription'

  export default {
    data() {
      return {
        takeMedicineVisible: false,
        dataList: [],
        visible: false,
        dataForm: {
          id: 0,
          presBillId: '',
          name: '',
          status: '',
          medicineStatus: '',
          presonName: '',
          presonId: '',
          visitTime: '',
          createTime: '',
          updateTime: '',
          createUser: '',
          modifyUser: '',
          tenantId: '',
          orgId: '',
          unitId: '',
          delFlag: ''
        },
        dataRule: {
          presBillId: [{
            required: true,
            message: '处方单号不能为空',
            trigger: 'blur'
          }],
          name: [{
            required: true,
            message: '药品名称不能为空',
            trigger: 'blur'
          }],
          status: [{
            required: true,
            message: '处方状态(1-合格处方,2-不合格处方)不能为空',
            trigger: 'blur'
          }],
          medicineStatus: [{
            required: true,
            message: '发药状态(1-未发药,2-以发药)不能为空',
            trigger: 'blur'
          }],
          presonName: [{
            required: true,
            message: '患者姓名不能为空',
            trigger: 'blur'
          }],
          presonId: [{
            required: true,
            message: '患者ID不能为空',
            trigger: 'blur'
          }],
          visitTime: [{
            required: true,
            message: '就诊时间不能为空',
            trigger: 'blur'
          }],
          createTime: [{
            required: true,
            message: '创建时间不能为空',
            trigger: 'blur'
          }],
          updateTime: [{
            required: true,
            message: '修改时间不能为空',
            trigger: 'blur'
          }],
          createUser: [{
            required: true,
            message: '创建用户id不能为空',
            trigger: 'blur'
          }],
          modifyUser: [{
            required: true,
            message: '修改用户id不能为空',
            trigger: 'blur'
          }],
          tenantId: [{
            required: true,
            message: '租户id不能为空',
            trigger: 'blur'
          }],
          orgId: [{
            required: true,
            message: '组织id不能为空',
            trigger: 'blur'
          }],
          unitId: [{
            required: true,
            message: '单位id不能为空',
            trigger: 'blur'
          }],
          delFlag: [{
            required: true,
            message: '逻辑删除(1-正常,-1已删除)不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          if (this.dataForm.id) {
            getPrescriptionObj(this.dataForm.id).then(response => {
              this.dataForm = response.data.data
              this.dataList = response.data.data.detailList
              console.log(response.data.data.detailList)
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putEimsOutpatientPrescriptionObj(this.dataForm).then(data => {
                this.$message.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              });
            } else {
              addEimsOutpatientPrescriptionObj(this.dataForm).then(data => {
                this.$message.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              })
            }
          }
        })
      },
      //确定发药
      takeMedicine() {
        this.dataForm.medicineStatus = 3;
        putEimsOutpatientPrescriptionObj(this.dataForm).then(data => {
          this.$message.success('发药成功')
          this.visible = false
        });
      }
    }
  }
</script>
